package zee.com.customviewexample

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import com.github.aachartmodel.aainfographics.aachartcreator.*
import com.github.aachartmodel.aainfographics.aaoptionsmodel.*
import com.github.aachartmodel.aainfographics.aatools.AAGradientColor
import com.github.aachartmodel.aainfographics.aatools.AALinearGradientDirection
import kotlinx.android.synthetic.main.activity_chait.*
import java.lang.StringBuilder

class ChartMainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_chait)
        view_AAChartView.aa_drawChartWithChartOptions(customDoubleXAxesChart())
    }

    companion object {
        fun customDoubleXAxesChart(): AAOptions {

            val aaChart = AAChart()
                    .type(AAChartType.Column)

            val aaTitle = AATitle()
                    .text("")
                    .style(AAStyle()
                            .color("#000000")
                            .fontSize(12.0f))


            val aaXAxis1 = AAXAxis()
                    .categories(arrayOf("周一", "周二", "周三", "周四", "周五", "周六", "周日"))
                    .labels(AALabels()
                            .step(1))

            val aaYAxisA = AAYAxis()
                    .gridLineWidth(1f)// Y 轴网格线宽度
                    .visible(true)
                    .title(AATitle()
                            .text(""))//Y 轴标题
                    .tickPositions(arrayOf(0, 1, 2, 3, 4, 5, 6, 7))
                    .min(1.0f)
                    .max(7.0f)

            val aaYAxisB = AAYAxis()
                    .gridLineWidth(0f)// Y 轴网格线宽度
                    .visible(true)
                    .title(AATitle()
                            .text(""))//Y 轴标题
                    .tickPositions(arrayOf(0, 50, 100, 150, 200, 250, 300, 350, 400, 450, 500))
                    .min(0f)
                    .max(500f)
                    .opposite(true)

//            val aaPlotOptions = AAPlotOptions()
//                    .series(AASeries()
//                            .animation(AAAnimation()
//                                    .duration(800)
//                                    .easing(AAChartAnimationType.Bounce))
//                            .stacking(AAChartStackingType.Normal))
            val valueA = arrayOf<Any>(10, 200, 30, 10, 400, 50, 60)
            val valueB = arrayOf<Any>(10, 200, 30, 10, 400, 50, 60)


            val aaTooltip = AATooltip()
                    .useHTML(true)
                    .shared(false)
//
//                    .formatter(
//                            """
//function () {
//
//let colorDot1 = '<span style=\"' + 'color:#36CFC9; font-size:13px\"' + '>◉</span> ';
//let 重量A = ${javaScriptArrayStringWithJavaArray(valueA)};
//let 重量B = ${javaScriptArrayStringWithJavaArray(valueB)};
//let 结束 = "<br/>";
//let 间隔 = "&nbsp&nbsp&nbsp&nbsp&nbsp";
//let 第几次 = colorDot1 + "第" + this.point.index + '次辅食' + 结束;
//
//let 数据A = 间隔 + "重量: &nbsp" + 重量A[this.point.index] + '今天' + 结束;
//let 数据B = 间隔 + "次数: &nbsp" + 重量B[this.point.index] + '昨天' + 结束;
//let result =  第几次 + 数据A + 数据B;
//    return result;
//}
//                """.trimIndent()
//                    )
                    .formatter(getJSValue())


            val aaSeriesElement1 = AASeriesElement()
                    .name("次数")
                    .color("#FFA246")
                    .yAxis(0)
                    .type(AAChartType.Scatter)
                    .showInLegend(true)
                    .allowPointSelect(false)
                    .data(arrayOf(1, 2, 3, 4, 3, 1, 0))

            val aaSeriesElement2 = AASeriesElement()
                    .name("重量")
                    .yAxis(1)
                    .type(AAChartType.Column)
                    .color("#36CFC9")
                    .showInLegend(true)
                    .allowPointSelect(false)
                    .data(arrayOf(100, 200, 300, 500, 100, 300, 400))

            return AAOptions()
                    .chart(aaChart)
                    .title(aaTitle)
                    .xAxis(aaXAxis1)
                    .yAxisArray(arrayOf(aaYAxisA, aaYAxisB))
                    .tooltip(aaTooltip)
                    .series(arrayOf(aaSeriesElement2, aaSeriesElement1))
        }

        fun getJSValue(): String {
            val jsValue = StringBuilder()
            val valueA = arrayOf<Any>(10, 200, 30, 10, 400, 50, 60)
            val valueB = arrayOf<Any>(1, 2, 3, 1, 2, 3, 1)

            jsValue.append("function () {")
            jsValue.append("let  linbreak = \"<br/>\";")
            jsValue.append("let gap = \"&nbsp&nbsp&nbsp&nbsp&nbsp\";\n")
            jsValue.append("let circlesA = '<span style=\\\"' + 'color:#36CFC9; font-size:13px\\\"' + '>◉</span> ' + linbreak ;")
            jsValue.append("let circlesB = '<span style=\\\"' + 'color:#36CFC9; font-size:13px\\\"' + '>◉</span> ';")
            jsValue.append("let 重量A = ${javaScriptArrayStringWithJavaArray(valueA)};")
            jsValue.append("let 重量B = ${javaScriptArrayStringWithJavaArray(valueB)};")
            jsValue.append("let 数据A = gap + \"重量: &nbsp\" + 重量A[this.point.index] + '今天' + linbreak;")
            jsValue.append("let 数据B = gap + \"次数: &nbsp\" + 重量B[this.point.index] + '今天' + linbreak;")
            jsValue.append("let result = circlesA +  数据A + 数据B;")
            jsValue.append("return result;")

            jsValue.append("}")


            return jsValue.toString()
        }

        private fun javaScriptArrayStringWithJavaArray(javaArray: Array<Any>): String {
            var originalJsArrStr = ""
            for (i in javaArray!!.indices) {
                val element = javaArray[i]
                originalJsArrStr = "$originalJsArrStr'$element',"
            }
            return "[$originalJsArrStr]"
        }

        fun configureColumnrangeMixedLineChart(): AAChartModel {
            return AAChartModel()
                    .colorsTheme(arrayOf("#1e90ff", "#EA007B", "#49C1B6", "#FDC20A", "#F78320", "#068E81"))//主题颜色数组
                    .title("")//图形标题
                    .subtitle("")//图形副标题
                    .chartType(AAChartType.Scatter)
                    .dataLabelsEnabled(false)
                    .markerSymbolStyle(AAChartSymbolStyleType.BorderBlank)
                    .series(arrayOf(
                            AASeriesElement()
                                    .name("Temperature")
                                    .type(AAChartType.Column)  //COLUMN_RANGE
                                    .data(arrayOf(
                                            9.4,
                                            6.5,
                                            9.4,
                                            19.9,
                                            22.6,
                                            29.5,
                                            30.7,
                                            26.5,
                                            18.0,
                                            11.4,
                                            10.4,
                                            16.8
                                    )),
                            AASeriesElement()
                                    .name("Berlin")
                                    .data(arrayOf(0.9, 0.6, 3.5, 8.4, 13.5, 17.0, 18.6, 17.9, 14.3, 9.0, 3.9, 1.0))
                    ))
        }

        internal fun configureAreaChartThreshold(): AAChartModel {

            val aaMarker = AAMarker()
            aaMarker.lineWidth(4.0f).radius(4.0f).fillColor("#ACABAE")
            return AAChartModel()
                    .chartType(AAChartType.Column)
                    .title("")
                    .yAxisTitle("")//设置Y轴标题
                    .yAxisGridLineWidth(0f)

                    .categories(arrayOf("周一", "周二", "周三", "周四", "周五", "周六", "周日"))
                    .dataLabelsEnabled(false)//是否显示值
                    .markerRadius(4f)
                    .markerSymbol(AAChartSymbolType.Circle)
                    .markerSymbolStyle(AAChartSymbolStyleType.Normal)
                    .legendEnabled(false)
                    .series(arrayOf(
                            AASeriesElement()
                                    .name("Tokyo").marker(aaMarker)
                                    .data(
                                            arrayOf(
                                                    7.0,
                                                    6.9,
                                                    9.5,
                                                    14.5,
                                                    18.2,
                                                    21.5,
                                                    25.2,
                                                    26.5,
                                                    23.3,
                                                    18.3,
                                                    13.9,
                                                    9.6
                                            )
                                    ).lineWidth(1f)
                                    .color("#ACABAE"),
                            AASeriesElement()
                                    .name("NewYork").marker(aaMarker)
                                    .data(arrayOf(
                                            0.2,
                                            0.8,
                                            5.7,
                                            11.3,
                                            17.0,
                                            22.0,
                                            24.8,
                                            24.1,
                                            20.1,
                                            14.1
                                    )).lineWidth(1f)
                                    .color("#ACABAE"),
                            AASeriesElement()
                                    .name("London").marker(aaMarker)
                                    .data(arrayOf(
                                            0.9,
                                            0.6,
                                            3.5,
                                            8.4,
                                            13.5,
                                            17.0,
                                            18.6,
                                            17.9,
                                            14.3,
                                            9.0
                                    )).lineWidth(1f)
                                    .color("#ACABAE")
                    ))
        }
    }
}